# ifndef __Reg_H_
# define __Reg_H_

class CRegistryKey {
  public:
	CRegistryKey () : m_h (NULL) {}
	CRegistryKey (HKEY _hKey, LPCTSTR _strSubKey) :
		m_h (NULL)
	{
		::RegOpenKey (_hKey, _strSubKey, & m_h);
	}
	~CRegistryKey () {
		if (m_h)
			::RegCloseKey (m_h);
	}

	bool IsValid () const {return m_h != NULL;}

	operator HKEY () const {return m_h;}

	LONG Create (HKEY _hKey, LPCTSTR _strSubKey) {
		assert (m_h == NULL);
		return ::RegCreateKey (_hKey, _strSubKey, & m_h);
	}
	LONG Open (HKEY _hKey, LPCTSTR _strSubKey) {
		assert (m_h == NULL);
		return ::RegOpenKey (_hKey, _strSubKey, & m_h);
	}

	LONG QueryValue (const char * _strName, char * _strValue, size_t _cSize) {
		assert (m_h != NULL);
		DWORD dwType = 0;
		DWORD dwSize = _cSize;
		return ::RegQueryValueEx (m_h, _strName, 0, & dwType, reinterpret_cast<BYTE *> (_strValue), & dwSize);
	}

	LONG SetValue (const char * _strName, const char * _strValue) {
		assert (m_h != NULL);
		return ::RegSetValueEx (m_h, _strName, 0, REG_SZ, reinterpret_cast<const BYTE *> (_strValue), _strValue ? ::strlen (_strValue) + 1 : 0);
	}

  private:
	HKEY m_h;
};

# endif // __Reg_H_